package cn.schoolwow.quickdao.test.module.business.user.ddl.testcase;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.database.parent.DatabaseType;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickdao.test.entity.User;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import org.junit.Assert;

public class ChangeNumberColumnTypeTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        flowContext.executeFlowList(new CreateNumberColumnTestCaseFlow());
        checkDatabase(flowContext);
        changeNumberColumnType(flowContext);
    }

    @Override
    public String name() {
        return "修改字段列类型测试用例";
    }

    private void checkDatabase(FlowContext flowContext){
        DatabaseType databaseType = flowContext.checkInstanceData(DatabaseType.class);

        switch (databaseType){
            case SQLite:{
                flowContext.brokenCurrentFlow("SQLite不支持修改列");
            }break;
        }
    }

    private void changeNumberColumnType(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);

        Entity entity = dao.getEntity(User.class);
        Property property = dao.getProperty(User.class, "number");

        property.columnType = "varchar";
        property.range = "12";
        property.notNull = false;

        dao.alterColumn(entity.tableName, property);

        Property alterProperty = dao.getProperty(User.class, "number");
        Assert.assertEquals(property.columnType, alterProperty.columnType);
        Assert.assertEquals(property.notNull, alterProperty.notNull);
    }
}
